﻿// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_PROD_BLUEPRINT_PRINT_HISTORY
// * CreateDate    : 2012/3/22 14:45:31
// * Creater       : CodeSmith
// * LastChangeDate: 2012/3/22 14:45:31
// * LastChanger   : 2012/3/22 14:45:31
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;

namespace ESOL.Business.Prod
{
    /// <summary>
    /// ProdBlueprintPrintHistory
    /// </summary>
    public partial class ProdBlueprintPrintHistoryBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objProdBlueprintPrintHistoryEntity">Entity</param>
        /// <returns>Entity</returns>
        public static ProdBlueprintPrintHistoryEntity GetProdBlueprintPrintHistory(ProdBlueprintPrintHistoryEntity objProdBlueprintPrintHistoryEntity)
        {
            return DataAccess.SelectSingle<ProdBlueprintPrintHistoryEntity>(objProdBlueprintPrintHistoryEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objProdBlueprintPrintHistoryEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertProdBlueprintPrintHistory(ProdBlueprintPrintHistoryEntity objProdBlueprintPrintHistoryEntity)
        {
            return DataAccess.Insert(objProdBlueprintPrintHistoryEntity);
        }

        public static bool InsertProdBlueprintPrintHistory(List<ProdBlueprintPrintHistoryEntity> objProdBlueprintPrintHistoryEntityList)
        {
            return DataAccess.Insert(objProdBlueprintPrintHistoryEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objProdBlueprintPrintHistoryEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateProdBlueprintPrintHistory(ProdBlueprintPrintHistoryEntity objProdBlueprintPrintHistoryEntity)
        {
            return DataAccess.Update(objProdBlueprintPrintHistoryEntity);
        }

        public static bool UpdateProdBlueprintPrintHistory(List<ProdBlueprintPrintHistoryEntity> objProdBlueprintPrintHistoryEntityList)
        {
            return DataAccess.Update<ProdBlueprintPrintHistoryEntity>(objProdBlueprintPrintHistoryEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objProdBlueprintPrintHistoryEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteProdBlueprintPrintHistory(ProdBlueprintPrintHistoryEntity objProdBlueprintPrintHistoryEntity)
        {
            return DataAccess.Delete(objProdBlueprintPrintHistoryEntity);
        }

        public static bool DeleteProdBlueprintPrintHistory(List<ProdBlueprintPrintHistoryEntity> objProdBlueprintPrintHistoryEntityList)
        {
            return DataAccess.Delete<ProdBlueprintPrintHistoryEntity>(objProdBlueprintPrintHistoryEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetProdBlueprintPrintHistoryAll()
        {
            return DataAccess.SelectAllDataSet<ProdBlueprintPrintHistoryEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<ProdBlueprintPrintHistoryEntity> GetProdBlueprintPrintHistoryAllList()
        {
            return DataAccess.SelectAll<ProdBlueprintPrintHistoryEntity>();
        }
        #endregion

        #region Functions
        public static bool SavePrintHistory(ProdBlueprintPrintHistoryEntity mBlueprintPrintHistory, ProdPlanOrderItemEntity mPlanOrderItem)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    DataAccess.Insert(mBlueprintPrintHistory, broker);
                    DataAccess.Update(mPlanOrderItem, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }

        public static int GetPrintedTotalQtyByPlanOrderItemID(string PlanOrderItemID)
        {
            string Sql = @"select (case when SUM(PRINT_QTY) is not  null then SUM(PRINT_QTY) else 0 end) as PRINTED_TOTAL_QTY 
                from dbo.PROD_BLUEPRINT_PRINT_HISTORY
                where PRODUCT_PLAN_ORDER_ITEM_ID='" + PlanOrderItemID + "' ";
            Sql += "group by PRODUCT_PLAN_ORDER_ITEM_ID";
            DataSet ds = new DataSet();
            ds = DataAccess.SelectDataSet(Sql);
            if (ds.Tables[0].Rows.Count == 0)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString());
            }
        }
        #endregion
    }


}

